from dal.es_dal import EsBaseDal


class BanksDal(EsBaseDal):

    def __init__(self):
        super(BanksDal, self).__init__(index="banks")

    # 根据银行名称前缀匹配银行信息
    async def prefix_match_bank_name(self, bank_name_ch: str):
        if not bank_name_ch:
            return ""

        values = bank_name_ch.split('银行')
        if len(values) > 1:
            values[0] = values[0] + "银行"

        bank_infos = await self.model.get_doc(
            {
                "query": {
                    "wildcard": {
                        "bank_name_ch.keyword": {
                            "value": f"{values[0]}*"
                        }
                    }
                }
            },
            size=1
        )

        return {} if not bank_infos else bank_infos[0]


if __name__ == '__main__':
    bank_es_dal = BanksDal()
    import asyncio
    loop = asyncio.get_event_loop()
    print(loop.run_until_complete(bank_es_dal.prefix_match_bank_name("中")))
